----------------------------------------------------------------------------------
-- Company: 
-- Engineer: Tian Jiashen
-- 
-- Create Date:    19:07:26 11/28/2014 
-- Design Name: 
-- Module Name:    extender - Behavioral 
-- Project Name: 
-- Target Devices: 
-- Tool versions: 
-- Description: 
--
-- Dependencies: 
--
-- Revision: 
-- Revision 0.01 - File Created
-- Additional Comments: 
--
----------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

-- Uncomment the following library declaration if using
-- arithmetic functions with Signed or Unsigned values
--use IEEE.NUMERIC_STD.ALL;

-- Uncomment the following library declaration if instantiating
-- any Xilinx primitives in this code.
--library UNISIM;
--use UNISIM.VComponents.all;

entity extender is
    Port ( imm : in  STD_LOGIC_VECTOR (10 downto 0);
           immCode : in  STD_LOGIC_VECTOR (2 downto 0);
           extension : out  STD_LOGIC_VECTOR (15 downto 0));
end extender;

architecture Behavioral of extender is
begin
process(imm, immCode)
begin
	case immCode is
	when "000"=> --no imm
		extension(15 downto 0) <= "0000000000000000";--maybe it can be put into others
	when "001"=> --4 bit imm
		if imm(3 downto 3) = "0" then
			extension(3 downto 0) <= imm(3 downto 0);
			extension(15 downto 4) <= "000000000000";
		elsif imm(3 downto 3) = "1" then
			extension(3 downto 0) <= imm(3 downto 0);
			extension(15 downto 4) <= "111111111111";
		end if;
	when "010"=> --5 bit imm
		if imm(4 downto 4) = "0" then
			extension(4 downto 0) <= imm(4 downto 0);
			extension(15 downto 5) <= "00000000000";
		elsif imm(4 downto 4) = "1" then
			extension(4 downto 0) <= imm(4 downto 0);
			extension(15 downto 5) <= "11111111111";
		end if;
	when "011"=> --8 bit imm
		if imm(7 downto 7) = "0" then
			extension(7 downto 0) <= imm(7 downto 0);
			extension(15 downto 8) <= "00000000";
		elsif imm(7 downto 7) = "1" then
			extension(7 downto 0) <= imm(7 downto 0);
			extension(15 downto 8) <= "11111111";
		end if;
	when "100"=> --11 bit imm
		if imm(10 downto 10) = "0" then
			extension(10 downto 0) <= imm(10 downto 0);
			extension(15 downto 11) <= "00000";
		elsif imm(10 downto 10) = "1" then
			extension(10 downto 0) <= imm(10 downto 0);
			extension(15 downto 11) <= "11111";
		end if;
	when others=> --error!
		extension(15 downto 0) <= "0000000000000000";
	end case;
end process;
end Behavioral;

